package com.jz.community.moduleshoppingguide.neighbor.model;

import android.content.Context;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.JsonRequestBodyConverter;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.moduleshoppingguide.neighbor.bean.BlogsListBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.CircleMessageBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.CirclePersonsBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.CircleUserInfoBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.NearCircleListBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.PublishTypeBean;
import com.jz.community.moduleshoppingguide.neighbor.bean.UploadImagesBean;
import com.jz.community.moduleshoppingguide.neighbor.net.NeighborNetApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class NeighborModelImp implements NeighborModel {

    private Context mContext;

    public NeighborModelImp(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void initUserNeighbor(OnLoadListener<CircleMessageBean> onLoadListener) {
        Observable<CircleMessageBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .initUserNeighbor());
        new RxLoadingWrapper<CircleMessageBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initUserInfo(String userId, OnLoadListener<CircleUserInfoBean> onLoadListener) {
        Observable<CircleUserInfoBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .initUserInfo(userId));
        new RxLoadingWrapper<CircleUserInfoBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void circleDynamic(String circleId, int page, int size, OnLoadListener<BlogsListBean> onLoadListener) {
        Observable<BlogsListBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .circleDynamic(circleId, page, size));
        new RxLoadingWrapper<BlogsListBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void putFavorite(String blogId, OnLoadListener<BlogsListBean.EmbeddedBean.ContentBean.FavoriteBean> onLoadListener) {
        Observable<BlogsListBean.EmbeddedBean.ContentBean.FavoriteBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .putFavorite(blogId));
        new RxLoadingWrapper<BlogsListBean.EmbeddedBean.ContentBean.FavoriteBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initNeighborMsg(String circleId, String lat, String lon, OnLoadListener<CircleMessageBean> onLoadListener) {
        Observable<CircleMessageBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .initNeighborMsg(circleId, lat, lon));
        new RxLoadingWrapper<CircleMessageBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void addNeighbor(String circleId, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Map<String, String> params = new HashMap<>();
        params.put("circleId", circleId);
        Observable<BaseResponseInfo> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .addNeighbor(new JsonRequestBodyConverter<Map<String, String>>().convert(params)));
        new RxLoadingWrapper<BaseResponseInfo>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void quitNeighbor(String circleId, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Map<String, String> params = new HashMap<>();
        params.put("circleId", circleId);
        Observable<BaseResponseInfo> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .quitNeighbor(new JsonRequestBodyConverter<Map<String, String>>().convert(params)));
        new RxLoadingWrapper<BaseResponseInfo>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initNearNeighbor(String lat, String lon, int page, int size, OnLoadListener<NearCircleListBean> onLoadListener) {
        Observable<NearCircleListBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .initNearNeighbor(lat, lon, page, size));
        new RxLoadingWrapper<NearCircleListBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initPersons(String circleId, int page, int size, OnLoadListener<CirclePersonsBean> onLoadListener) {
        Observable<CirclePersonsBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .initPersonsData(circleId, page, size));
        new RxLoadingWrapper<CirclePersonsBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initTips(OnLoadListener<PublishTypeBean> onLoadListener) {
        Observable<PublishTypeBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .initTips());
        new RxLoadingWrapper<PublishTypeBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void uploadImages(ArrayList<File> files, OnLoadListener<UploadImagesBean> onLoadListener) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("uploadPic", file.getName(), requestBody);
            parts.add(part);
        }
        Observable<UploadImagesBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .uploadImages(parts));
        new RxLoadingWrapper<UploadImagesBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void publishMessage(String image_up, String content, String tip, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Map<String, String> params = new HashMap<>();
        params.put("blogImg", image_up);
        params.put("content", content);
        params.put("blogType", tip);

        Observable<BaseResponseInfo> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, NeighborNetApi.class)
                .publishMessage(new JsonRequestBodyConverter<Map<String, String>>().convert(params)));
        new RxLoadingWrapper<BaseResponseInfo>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }
}
